Skip to content

Build tree optimization#280

Open
vivaansinghvi07 wants to merge 4 commits intomasterfrom
build-tree-optimization
Open

Build tree optimization#280
vivaansinghvi07 wants to merge 4 commits intomasterfrom
build-tree-optimization

Conversation

@vivaansinghvi07
Copy link
Collaborator

@vivaansinghvi07 vivaansinghvi07 commented Feb 15, 2026

fixes the second part of #271

@mmore500
Copy link
Owner

Cherry picked code beautification/simplification to #315

@mmore500
Copy link
Owner

mmore500 commented Feb 24, 2026

I wonder if we could keep the children sorted but work around the bottleneck something like this?

  std::vector<u64> to_reattach;

  // drop children and attach grandchildren
  while (!node_stack.empty()) {
    const u64 popped_node = node_stack.back();
    node_stack.pop_back();
    detach_search_parent(records, popped_node);

    std::vector<u64> grandchildren;
    const auto grandchildren_range = ChildrenView(records, popped_node);
    std::ranges::copy(grandchildren_range, std::back_inserter(grandchildren));

    for (const u64 grandchild : grandchildren) {
      if (records.rank[grandchild] >= rank) {
        detach_search_parent(records, grandchild);
          to_reattach.push_back(grandchild);
      } else {
        node_stack.push_back(grandchild);
      }
    }
  }

  # not sure if this part is a good idea?
  # if (to_reattach.size() > 0) {
  #   for (const u64 child : ChildrenView(records, node)) {
  #     detach_search_parent(records, child);
  #     to_reattach.push_back(child);
  #   }
  # }

  std::sort(
    to_reattach.begin(), to_reattach.end(),
    [&records](const u64 lhs, const u64 rhs) {
      return (
        std::tuple{records.rank[lhs], lhs} < std::tuple{records.rank[rhs], rhs}
      );
    }
  );
  for (const u64 reattached : std::ranges::reverse_view(to_reattach)) {
    attach_search_parent(records, reattached, node);
  }

  collapse_indistinguishable_nodes(records, node);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants